<?php
namespace app\common;

use think\Controller;
use think\Facade\Request;
use think\Session;

use app\model\UserModel;

class BaseController extends Controller{

    protected $noNeedLogin=[];
    protected $user;
    protected $uid;

    protected $beforeActionList = [
        'validtoken'=>'',
    ];
    // public function _initialize(){
    //         $this->noNeedLogin = ['session_set',''];
    //     }


    
    /**
     * token验证
     */
    protected function validtoken(){
        //$request = new Request();
        $action = Request::instance()->action();
        $request_data=$this->request->param();
        var_dump($request_data);
var_dump(Request::instance()->routeInfo);
exit();
var_dump(Request::instance()->controller());
exit();
        // echo $action;
        // // echo ;
        // foreach($this->noNeedLogin as $item){
        //     echo $item;
        // }
        // die();
        // echo in_array($action,$this->noNeedLogin);
        if( in_array($action,$this->noNeedLogin) ){
            // echo '111';

            return;
            // echo '111';
            // die();
        }

        // echo '222';
        $token = Request::instance()->header('token');
        $token = empty($token)?Request::instance()->param('token'):$token;
        // $token = '89b266b6fdbdfa73d8e7e3aa5735e890ed5e917a';
        // var_dump($token);

        if(empty($token) ){
            echo json_encode(["message"=>"token不存在","code" => 400],JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);

            // return json_encode(['code' => $code, 'message' => $message, 'data' => $data], );
            die();
            // return ['message'=>'token不存在','code'=>'400'];
            // $this->redirect('api/errorHandle/error_response','code=400&message=token不存在');

        }

        $openid = Session::get('Session_User_'.$token);
        if( empty($openid) ){
            echo json_encode(["message"=>urldecode("token已过期"),"code" => 400],JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
            die();
            // $this->redirect('api/errorHandle/error_response','code=400&message=token已过期');
        }

        $userData = UserModel::where('openid','eq',$openid)->find();
        // var_dump($userData);

        if( empty($userData) ){
            echo json_encode(["message"=>"用户不存在","code" => 400],JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
            die();
            // $this->redirect('api/errorHandle/error_response','code=400&message=用户不存在'.$openid);
        }

        
        $this->user = $userData;
        $this->uid = $userData->id;

    }
}